package com.newsnetwork.article.controller;

import com.newsnetwork.pojo.Article;
import com.newsnetwork.pojo.Spouse;
import com.newsnetwork.pojo.Stu;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * @author HuangDekai
 * @date 2022/11/16 17:17
 * @Description: 测试 FreeMarker
 */
@Controller
@RequestMapping("free")
public class FreeMarkerController {

    private final static Logger logger = LoggerFactory.getLogger(FreeMarkerController.class);

    @Value("${freemarker.html.target}")
    private String htmlTarget;

    @GetMapping("/createHTML")
    @ResponseBody
    public String createHTML(Model model) throws IOException, TemplateException {
        // 配置freemarker基本环境
        Configuration cfg = new Configuration(Configuration.getVersion());
        // 声明freemarker模板所需要加载的目录的位置
        String classpath = this.getClass().getResource("/").getPath();
        cfg.setDirectoryForTemplateLoading(new File(classpath + "templates"));

        //获得模板ftl文件
        Template template = cfg.getTemplate("helloObject.ftl", "utf-8");

        //获得动态数据
        model = makeModel(model);

        //融合动态数据和ftl, 生成html
        File tempDic = new File(htmlTarget);
        if (!tempDic.exists()) {
            tempDic.mkdirs();
        }

        FileWriter out = new FileWriter(htmlTarget + File.separator + "10010" + ".html");
        template.process(model, out);
        out.close();
        return "OK";
    }

    @GetMapping("/hello")
    public String hello(Model model) {
        // 定义输出到模板的内容
        String stranger = "新闻网 newsnetwork.com";
        // 这里的 networkName 是模板里 ${} 设置的属性名
        model.addAttribute("networkName", stranger);

        // 返回的 hello 是指freemarker模板所在的目录位置 classpath:/templates/
        // 后缀名已经由配置文件配置,是 .ftl
        return "hello";
    }

    @GetMapping("/helloObject")
    public String helloObject(Model model) {
        makeModel(model);
        return "helloObject";
    }

    private Model makeModel(Model model) {
        Stu stu = new Stu();
        stu.setUid("10010");
        stu.setUsername("test");
        stu.setAmount(100.86f);
        stu.setAge(25);
        stu.setHaveChild(true);
        stu.setBirthday(new Date());

        Spouse spouse = new Spouse();
        spouse.setUsername("test_spouse");
        spouse.setAge(21);
        stu.setSpouse(spouse);

        List<Article> articleList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Article article = new Article();
            article.setId("100"+i);
            article.setTitle("Title" + i);
            articleList.add(article);
        }
        stu.setArticleList(articleList);

        Map<String, String> parents = new HashMap<>();
        parents.put("father", "李雷");
        parents.put("mother", "韩梅梅");
        stu.setParents(parents);

        model.addAttribute("stu", stu);

        return model;
    }
}
